package com.tryedhp.signtool;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.tryedhp.signtool.myapplication.R;

import java.util.List;

import me.zhouzhuo.zzletterssidebar.adapter.BaseSortRecyclerViewAdapter;
import me.zhouzhuo.zzletterssidebar.viewholder.BaseRecyclerViewHolder;

/**
 * Created by zz on 2016/8/17.
 */
public class PersonRecyclerViewAdapter extends BaseSortRecyclerViewAdapter<AppInfoModel, BaseRecyclerViewHolder> {

    public PersonRecyclerViewAdapter(Context ctx, List<AppInfoModel> mDatas) {
        super(ctx, mDatas);
    }

    //return your itemView layoutRes id
    @Override
    public int getItemLayoutId() {
        return R.layout.rvitem_appinfo;
    }

    //add a header ,optional, if no need, return 0
    @Override
    public int getHeaderLayoutId() {
        return 0;
    }

    //add a footer, optional, if no need, return 0
    @Override
    public int getFooterLayoutId() {
        return 0;
    }

    @Override
    public BaseRecyclerViewHolder getViewHolder(View itemView, int type) {
        switch (type) {
            case BaseSortRecyclerViewAdapter.TYPE_HEADER:
                return new HeaderHolder(itemView);
            case BaseSortRecyclerViewAdapter.TYPE_FOOT:
                return new FooterHolder(itemView);
        }
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {

        if (holder instanceof MyViewHolder) {
            //must add this
            final int mPos = position - getHeadViewSize();
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            try {
                initLetter(holder, position);
                try {
                    myViewHolder.icon.setImageDrawable(mDatas.get(position).getIcon());
                } catch (Exception e) {
//                    e.printStackTrace();
                }
                myViewHolder.tv_appName.setText(mDatas.get(position).getName());
                myViewHolder.tv_appIsSystem.setText("是否系统应用：" + mDatas.get(position).isSystem());
                myViewHolder.tv_appVersionCode.setText("版本号：" + mDatas.get(position).getVersionCode());
                myViewHolder.tv_appVersionName.setText("版本名称：" + mDatas.get(position).getVersionName());
                myViewHolder.tv_appPackageName.setText("包名：" + mDatas.get(position).getPackageName());
                myViewHolder.tv_appPackagePath.setText("包路径：" + mDatas.get(position).getPackagePath());
                myViewHolder.tv_appMD5.setText("MD5：" + mDatas.get(position).getMD5());
                myViewHolder.tv_appMD5LowerCase.setText("MD5小写：" + mDatas.get(position).getMD5().replaceAll(":", "").toLowerCase());
                myViewHolder.tv_appSHA1.setText("SHA1：" + mDatas.get(position).getSHA1());
                myViewHolder.tv_appSHA256.setText("SHA256：" + mDatas.get(position).getSHA256());
                myViewHolder.btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).toString()));
                        ToastUtils.showLong("全部信息复制成功!");
                    }
                });
                myViewHolder.tv_appName.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getName()));
                        ToastUtils.showLong("App名字复制成功!");

                    }

                });
                myViewHolder.tv_appVersionCode.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getVersionCode() + ""));
                        ToastUtils.showLong(mDatas.get(position).getName() + "版本号复制成功!");

                    }

                });
                myViewHolder.tv_appVersionName.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getVersionName() + ""));
                        ToastUtils.showLong(mDatas.get(position).getName() + "版本名称复制成功!");

                    }

                });
                myViewHolder.tv_appPackageName.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getPackageName() + ""));
                        ToastUtils.showLong(mDatas.get(position).getName() + "包名复制成功!");

                    }

                });
                myViewHolder.tv_appPackagePath.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getPackagePath() + ""));
                        ToastUtils.showLong(mDatas.get(position).getName() + "包路径复制成功!");

                    }

                });
                myViewHolder.tv_appMD5.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getMD5() + ""));
                        ToastUtils.showLong(mDatas.get(position).getName() + " MD5复制成功!");

                    }

                });
                myViewHolder.tv_appMD5LowerCase.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getMD5().replaceAll(":", "").toLowerCase()));
                        ToastUtils.showLong(mDatas.get(position).getName() + " MD5小写复制成功!");
                    }
                });
                myViewHolder.tv_appSHA1.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getSHA1() + ""));
                        ToastUtils.showLong(mDatas.get(position).getName() + " SHA1复制成功!");

                    }

                });
                myViewHolder.tv_appSHA256.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) inflater.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(ClipData.newPlainText("text", mDatas.get(position).getSHA256() + ""));
                        ToastUtils.showLong(mDatas.get(position).getName() + " SHA256复制成功!");

                    }

                });
                initClickListener(holder, position);
            } catch (Exception e) {
                ToastUtils.showLong("发生未知错误" + e.getMessage());
            }
        } else if (holder instanceof HeaderHolder) {

        } else if (holder instanceof FooterHolder) {
            ((FooterHolder) holder).tvFoot.setText(getContentCount() + "位联系人");
        }

    }

    //custom your ViewHolder here
    public static class MyViewHolder extends BaseRecyclerViewHolder {

        protected TextView tv_appName;
        protected TextView tv_appIsSystem;
        protected TextView tv_appVersionCode;
        protected TextView tv_appVersionName;
        protected TextView tv_appPackageName;
        protected TextView tv_appPackagePath;
        protected TextView tv_appMD5;
        protected TextView tv_appMD5LowerCase;
        protected TextView tv_appSHA1;
        protected TextView tv_appSHA256;
        protected Button btn;
        protected ImageView icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_appName = itemView.findViewById(R.id.tv_appName);
            tv_appIsSystem = itemView.findViewById(R.id.tv_appIsSystem);
            tv_appVersionCode = itemView.findViewById(R.id.tv_appVersionCode);
            tv_appVersionName = itemView.findViewById(R.id.tv_appVersionName);
            tv_appPackageName = itemView.findViewById(R.id.tv_appPackageName);
            tv_appPackagePath = itemView.findViewById(R.id.tv_appPackagePath);
            tv_appMD5 = itemView.findViewById(R.id.tv_appMD5);
            tv_appMD5LowerCase = itemView.findViewById(R.id.tv_appMD5LowerCase);
            tv_appSHA1 = itemView.findViewById(R.id.tv_appSHA1);
            tv_appSHA256 = itemView.findViewById(R.id.tv_appSHA256);
            btn = itemView.findViewById(R.id.btn);
            icon = itemView.findViewById(R.id.icon);
        }
    }

    public static class HeaderHolder extends BaseRecyclerViewHolder {

        public HeaderHolder(View itemView) {
            super(itemView);
        }
    }

    public static class FooterHolder extends BaseRecyclerViewHolder {

        protected TextView tvFoot;

        public FooterHolder(View itemView) {
            super(itemView);
        }
    }

}
